#pragma once
/// Copyright 2012, Alex Averian, Yonan Research
/// Released under BSD Licence



namespace micro
{
namespace qzip
{

#define TAR_MAXNAMELEN 100
#define TAR_MAXPATHLEN 255
#define TAR_BLOCKSIZE  512

class QTarFile
{
protected:

// The official TAR archive block structure. This structure is found in the .tar
// archive preceding any directory and file entry. There is no actual header structure
// describing the file itself - it merely ends with 2 empty blocks.
// All  numeric  fields  contain  size-1  leading   zero-filled
// numbers  using  octal  digits.   They are followed by one or
// more space or null characters.  All  recent  implementations
// only use one space or null character at the end of a numerical field
// to get maximum space for the octal  number.


    typedef struct tagTAR_HEADER
    {
        char name[100];
        char mode[8];
        char uid[8];
        char gid[8];
        char size[12];
        char mtime[12];
        char chksum[8];
        char typeflag;
        char linkname[100];
        char magic[6];
        char version[2];
        char uname[32];
        char gname[32];
        char devmajor[8];
        char devminor[8];
        char prefix[155];
        char junk[12];
    } TAR_HEADER;


    QObArray<WIN32_FIND_DATA> m_Files;			  // Cache of files in archive

public:
    QTarFile();
    ~QTarFile();


    //tar2 Create
    bool		CreateArchive2( LPCTSTR pstrFilename );
    bool		AddFolder2( LPCTSTR label, LPCTSTR dir );
	bool		AddFolder2Intern( LPCTSTR label );

	bool		AddFile2( LPCTSTR label, LPCTSTR path );
    bool		AddFile2Intern( LPCTSTR label, const LPBYTE pbBuffer, DWORD dwFileSize, DWORD dwAttributes );
    bool		CloseArchive2();


    //tar3		Enum/Extract/Rename/Add/Delete
    bool		OpenArchive3( LPCTSTR file );
    bool		CloseArchive3();
    bool		ExtractFile( LPCTSTR file, LPCTSTR dest, void * progress );
    bool		ExtractFolder( QString & srcDir, QString & dest, void * progress );
    bool		GetFilelist( LPCTSTR pstrPath, QObArray<WIN32_FIND_DATA> & aList );
    bool		GetFileData( LPCTSTR pstrFilename, WIN32_FIND_DATA & wfd );

    //tar1

    bool		DeleteFile( LPCTSTR pstrFilename );
 

protected:

    //utils
    time_t		GetUnixTime();
    DWORD		GetFromOctalText( LPCSTR pstr, size_t cchMax );

    void		SetOctalText( LPSTR pstr, size_t cchMax, DWORD dwValue );
    DWORD		GetRoundPosition( DWORD dwPos );

    void		GetFullpath( const TAR_HEADER & Header, LPSTR pstrPath, char chSep );
    bool		GetFolderPath( LPCTSTR file, LPTSTR pstrPath, TCHAR chSep );
    bool		GetFileName( LPCTSTR path, LPTSTR dest );
    void		MakeHeader( TAR_HEADER & Header, BOOL bIsNew, LPCSTR pstrFilename, DWORD dwFileSize, DWORD dwAttributes );


    bool		AddToList( WIN32_FIND_DATA & wfd );
    bool		ConvertFileInfo( const TAR_HEADER & pInfo, WIN32_FIND_DATA & wfd );


    //create/add
   


    //tar2
    QString		m_szFilename;
    QFile		m_file;			//create
    QFileMap	m_filemap;		//enum

};


}
}//